package com.begye.smartloan.apptest.homebefore

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.certify.CertifyActivity
import com.begye.smartloan.apptest.daohang.DaoHangBottom
import com.begye.smartloan.apptest.databinding.AtHomeBeforeBinding
import com.begye.smartloan.apptest.detailbefore.DetailBeforeActivity
import com.begye.smartloan.apptest.fangkuanzhong.FangKuanZhongActivity
import com.begye.smartloan.apptest.gongong.YongHuJiDingYiViewModel
import com.begye.smartloan.apptest.gongong.YongHuLiuChengViewModel
import com.begye.smartloan.apptest.huankuan.HuanKuanActivity
import com.begye.smartloan.apptest.huankuanbefore.HuanKuanBeforeActivity
import com.begye.smartloan.apptest.jujue.JuJueActivity
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.outbefore.OutRecordBeforeActivity
import com.begye.smartloan.apptest.renzheng.RenZhengActivity
import com.begye.smartloan.apptest.renzheng.bangka.RenZhengBangKaActivity
import com.begye.smartloan.apptest.renzheng.document.RenZhengDocumentActivity
import com.begye.smartloan.apptest.renzheng.lianxiren.RenZhengLianXiRenActivity
import com.begye.smartloan.apptest.renzheng.selfie.RenZhengSelfieActivity
import com.begye.smartloan.apptest.shenhezhong.ShenHeZhongActivity
import com.begye.smartloan.apptest.tool.PageTagTool
import com.begye.smartloan.apptest.tool.SysConfigTool
import com.begye.smartloan.apptest.tool.addTouch
import com.util.utillib.extend.IntentFlagMode
import com.util.utillib.extend.toNextActivity
import com.vmadalin.easypermissions.EasyPermissions


@SuppressLint("CustomWelcomeScreen")
class HomeBeforeActivity : AppCompatActivity() {

    private lateinit var _binding: AtHomeBeforeBinding

    private val _yongHuLiuChengViewModel by lazy {
        ViewModelProvider(this)[YongHuLiuChengViewModel::class.java]
    }

    private val _yongHuJiDingYiViewModel by lazy {
        ViewModelProvider(this)[YongHuJiDingYiViewModel::class.java]
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtHomeBeforeBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configYongHuLiuChengState()
        configUIState()
        configUIEvent()
    }

    private fun configYongHuLiuChengState() {

        _yongHuLiuChengViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuJiDingYiViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }

        _yongHuLiuChengViewModel.onHasOrderStatusLiveData.observe(this) {
            it?.let { result ->
                when (result) {
                    PageTagTool.dai_huan_kuan -> {
                        toNextActivity(
                            activity = HuanKuanActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.fang_kuan_zhong -> {
                        toNextActivity(
                            activity = FangKuanZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.shen_he_zhone -> {
                        toNextActivity(
                            activity = ShenHeZhongActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }

                    PageTagTool.ju_jue -> {
                        toNextActivity(
                            activity = JuJueActivity::class.java,
                            mode = IntentFlagMode.SINGLE_TOP
                        )
                    }
                }
            }
        }

        _yongHuLiuChengViewModel.onDoingRenZhengPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingLianXiRenPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengLianXiRenActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingBangKaFrPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengBangKaActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingDocumentPageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengDocumentActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onDoingSelfiePageLiveData.observe(this) {
            if (it) {
                toNextActivity(
                    activity = RenZhengSelfieActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuLiuChengViewModel.onAllDoneLiveData.observe(this) {
            if (it.isNotBlank()) {
                // to shen qing ye
                toNextActivity(
                    activity = DetailBeforeActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _yongHuJiDingYiViewModel.onCheckYongHuDataLiveData.observe(this) {
            it?.let { result ->

                val usedJinEInt = result.optInt("usedJinEInt", 0)
                if (usedJinEInt <= 0) {
                    _binding.atHomeBeforeRecords.visibility = View.GONE
                } else {
                    _binding.atHomeBeforeRecords.visibility = View.VISIBLE
                }

                val certify = result.optString("certify", "NO")
                if (certify == "YES") {
                    _binding.atHomeBeforeMaxJinEValue.text =
                        result.optString("remainJinEFormat", "")

                    val nextPDate = result.optString("nextPDate", "")
                    val usedJinEFormat = result.optString("usedJinEFormat", "")
                    _binding.atHomeBeforeTopStepResultNextDayValue.text = nextPDate
                    _binding.atHomeBeforeTopStepResultUtilisedValue.text = usedJinEFormat

                    if (nextPDate.isBlank()) {
                        _binding.atHomeBeforeTopStepResultNextDayValue.text = "--/--/----"
                    }
                    if (usedJinEFormat.isBlank()) {
                        _binding.atHomeBeforeTopStepResultUtilisedValue.text = "? ? ? ?"
                    }

                    _binding.atHomeBeforeMaxJinEValue.visibility = View.VISIBLE
                    _binding.atHomeBeforeMaxJinEHint.visibility = View.INVISIBLE

                    val remainJinEInt = result.optInt("remainJinEInt", 0)
                    if (remainJinEInt > 0) {
                        _binding.atHomeBeforeStart.isEnabled = true
                        _binding.atHomeBeforeStart.setTextColor(Color.parseColor("#BEFF6A"))
                        _binding.atHomeBeforeStart.setBackgroundResource(R.drawable.layer_c25_000000)
                    } else {
                        _binding.atHomeBeforeStart.isEnabled = false
                        _binding.atHomeBeforeStart.setTextColor(Color.parseColor("#FFFFFF"))
                        _binding.atHomeBeforeStart.setBackgroundResource(R.drawable.layer_c25_dedede)
                    }

                } else {
                    _binding.atHomeBeforeMaxJinEValue.visibility = View.INVISIBLE
                    _binding.atHomeBeforeMaxJinEHint.visibility = View.VISIBLE

                    _binding.atHomeBeforeTopStepResultNextDayValue.text = "--/--/----"
                    _binding.atHomeBeforeTopStepResultUtilisedValue.text = "? ? ? ?"

                    _binding.atHomeBeforeStart.isEnabled = true
                    _binding.atHomeBeforeStart.setTextColor(Color.parseColor("#BEFF6A"))
                    _binding.atHomeBeforeStart.setBackgroundResource(R.drawable.layer_c25_000000)
                }
            }
        }
    }

    private fun configUIState() {

    }

    @SuppressLint("SetTextI18n")
    private fun configUIEvent() {

        _binding.atHomeBeforeNav.initConfig(
            leftResId = R.drawable.dres_6,
            rightResId = R.drawable.dres_4
        ) {
            if (0 == it) {
                _binding.atHomeBeforeCouTiDrawer.refresh(this@HomeBeforeActivity) {
                    if (_binding.atHomeBeforeRootLayout.isDrawerOpen(_binding.atHomeBeforeCouTiDrawer)) {
                        _binding.atHomeBeforeRootLayout.closeDrawer(_binding.atHomeBeforeCouTiDrawer)
                    }
                }

                if (_binding.atHomeBeforeRootLayout.isDrawerOpen(_binding.atHomeBeforeCouTiDrawer)) {
                    _binding.atHomeBeforeRootLayout.closeDrawer(_binding.atHomeBeforeCouTiDrawer)
                } else {
                    _binding.atHomeBeforeRootLayout.openDrawer(_binding.atHomeBeforeCouTiDrawer)
                }
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        _binding.atHomeBeforeBottom.initConfig {
            if (it == DaoHangBottom.TYPE_HUAN_KUAN) {
                toNextActivity(
                    activity = HuanKuanBeforeActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }

        _binding.atHomeBeforeRecords.setOnClickListener {
            toNextActivity(
                activity = OutRecordBeforeActivity::class.java,
                mode = IntentFlagMode.NEW_TASK
            )
        }

        _binding.atHomeBeforeStart.setOnClickListener {
            //
            addTouch(type = "home_before_commit_click")

            if (_yongHuJiDingYiViewModel.isDidCertify()) {
                _yongHuLiuChengViewModel.requestYongHuLiuCheng()
            } else {
                toNextActivity(
                    activity = CertifyActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            }
        }
    }

    private fun checkPermission(): Boolean {
        val list = SysConfigTool.filterShouYeQuanXiang()
        return EasyPermissions.hasPermissions(
            this,
            *(list.toTypedArray<String>())
        )
    }

    override fun onResume() {
        super.onResume()
        _yongHuJiDingYiViewModel.requestYongHuJiDingYi()
        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}

